package com.atguigu.tingshu.order.pattern.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.order.pattern.AbstractTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component(SystemConstant.ORDER_ITEM_TYPE_TRACK)
public class TrackTrade extends AbstractTradeStrategy {

    @Autowired
    private AlbumFeignClient albumFeignClient;

    /**
     * 对声音进行结算
     * @param tradeVo
     * @param userId
     * @return
     */
    @Override
    public OrderInfoVo trade(TradeVo tradeVo, Long userId) {

        log.info("对声音进行结算");


        String itemType = tradeVo.getItemType();

        Long itemId = tradeVo.getItemId();

        Integer trackCount = tradeVo.getTrackCount();

        //先初始化原始数据
        OrderInfoVo orderInfoVo = new OrderInfoVo();

        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();

        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();

        //初始化原始订单金额
        BigDecimal originalAmount = new BigDecimal("0.00");

        //初始化折扣订单金额
        BigDecimal derateAmount = new BigDecimal("0.00");

        //初始化订单订单金额
        BigDecimal orderAmount = new BigDecimal("0.00");

        //4.处理购买项目类型：声音
        //4.1 远程调用专辑服务，获取未购买声音列表
        Long trackId = tradeVo.getItemId();
        List<TrackInfo> trackInfoList = albumFeignClient.findWaitBuyTrackList(trackId, tradeVo.getTrackCount()).getData();
        Assert.notNull(trackInfoList, "不存在待结算声音", trackId);

        //4.2 远程调用专辑服务获取，专辑价格（声音单价）声音不支持折扣
        AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(trackInfoList.get(0).getAlbumId()).getData();
        Assert.notNull(albumInfo, "专辑{}不存在", albumInfo.getId());
        BigDecimal price = albumInfo.getPrice();

        //4.3 计算相关价格
        originalAmount = price.multiply(new BigDecimal(trackInfoList.size()));
        orderAmount = originalAmount;

        //4.4 封装订单明细列表
        orderDetailVoList = trackInfoList.stream()
                .map(trackInfo -> {
                    OrderDetailVo orderDetailVo = new OrderDetailVo();
                    orderDetailVo.setItemId(trackInfo.getId());
                    orderDetailVo.setItemName("声音："+trackInfo.getTrackTitle());
                    orderDetailVo.setItemUrl(trackInfo.getCoverUrl());
                    orderDetailVo.setItemPrice(price);
                    return orderDetailVo;
                }).collect(Collectors.toList());

        //5.封装订单VO对象
        //5.1 封装相关价格信息
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        //5.2 封装商品相关集合
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        //封装类型
        orderInfoVo.setItemType(itemType);

        return orderInfoVo;
    }
}
